<?php
class model_content extends model
{
	function __construct()
	{
		parent::__construct();
		$this->_table = $this->db->options['prefix'].'content';
		$this->_primary = 'contentid';
		$this->_fields = array('contentid','missionid','title','url','passed','fetched','created');
		
		$this->_readonly = array('contentid');
		$this->_create_autofill = array('created'=>TIME);
	}

	function add($data)
	{
		return $this->insert($data);
	}
	
	function edit($missionid, $data)
	{
		return $this->update($data, $missionid);	
	}
	
	function get($contentid)
	{
		return parent::get($contentid);
	}
	
	function ls($where = null, $fields = '*', $order = '`#table_content`.`contentid` DESC', $page = null, $pagesize = null)
	{
		$this->where = $where;
		$this->fields = $fields;
		$this->order = $order;
		$this->page = $page;
		$this->pagesize = $pagesize;
		$this->sql =  "SELECT $this->fields FROM `#table_content`, `#table_mission` WHERE `#table_content`.`missionid`=`#table_mission`.`missionid`";
		if ($this->where) $this->sql .= ' AND '.$this->where;
		if ($this->order) $this->sql .= ' ORDER BY '.$this->order;
		$this->data = $this->db->page($this->sql, $this->page, $this->pagesize);
		if($this->data)
		{
			$this->total = $this->count($where);
		}
		return $this->data;
	}
	
	function delete($contentid)
	{
		return parent::delete($contentid);
	}
	
	function html_write($contentid)
	{
		$contentid = id_format($contentid);
		if ($contentid === false)
		{
			$this->error = "$contentid 格式不正确";
			return false;
		}
		if (is_array($contentid)) return array_map(array($this, 'html_write'), $contentid);
		
	}
}